package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("setmealDto{}}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("SetmealDto=>{}{}{}", page, pageSize, name);
        Page<SetmealDto> setmealDtoPage = setmealService.pageAll(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> updateById(@PathVariable Long id) {
        log.info("id=>{}", id);
        SetmealDto setmealDto = setmealService.updateId(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        log.info("setmealDto==> {}", setmealDto);
        setmealService.update(setmealDto);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List> list(Setmeal setmeal) {
        log.info("setmeal:{}", setmeal);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }


    @GetMapping("/dish/{id}")
    public R<List> dish(@PathVariable Long id) {
        log.info("id:{}", id);
        List list = setmealService.findDishById(id);
        return R.success(list);
    }

    @PostMapping("/status/{status}")
    public R<String> updateById(@PathVariable int status, long[] ids) {
        log.info("状态status：{}", status);
        log.info("起售状态ids：{}", ids);
        List<Setmeal> list = new ArrayList<>();
        for (long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            list.add(setmeal);
        }
        setmealService.saveOrUpdateBatch(list);
        return R.success("状态修改成功");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("ids:{}", ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }
}
